﻿Public Class ConfigFile
    Private pConfigFilePath As String = My.Application.Info.DirectoryPath & "\config.xml"
    Public Event FileUpdated(ByVal sender As Object, ByVal PropertyUpdated As Integer)

    Enum ConfigFileProperties
        NewFile = 1
        LastOpenDir = 2
        LastSaveDir = 3
        LastExportDir = 4
        RecentFileNumber = 5
        RecentFilesList = 6
    End Enum
    Public ReadOnly Property FilePath As String
        Get
            Return pConfigFilePath
        End Get
    End Property
    Public Property LastOpenDir As String
        Get
            Return ReadValue("lastopendir")
        End Get
        Set(ByVal value As String)
            SaveValue("lastopendir", value)
            RaiseEvent FileUpdated(Me, 2)
        End Set
    End Property
    Public Property LastExportDir As String
        Get
            Return ReadValue("lastexportdir")
        End Get
        Set(ByVal value As String)
            SaveValue("lastexportdir", value)
            RaiseEvent FileUpdated(Me, 4)
        End Set
    End Property
    Public Property LastSaveDir As String
        Get
            Return ReadValue("lastsavedir")
        End Get
        Set(ByVal value As String)
            SaveValue("lastsavedir", value)
            RaiseEvent FileUpdated(Me, 3)
        End Set
    End Property
    Public ReadOnly Property RecentFilesList() As Collection
        Get
            Dim pElencoFile = From el As XElement In XElement.Load(Me.FilePath).<recentfiles>.<file>
                  Select el
                  Order By DateTime.Parse(el.@date) Descending, DateTime.Parse(Left(el.@time, 2) & ":" & Mid(el.@time, 4, 2) & ":" & Right(el.@time, 2)) Descending
            Dim pRecentFileList As New Collection
            For Each Elemento As XElement In pElencoFile
                pRecentFileList.Add(Elemento.Value)
                If pRecentFileList.Count >= Me.RecentFileNumber Then
                    Exit For
                End If
            Next
            Return pRecentFileList
        End Get
    End Property
    Public Property RecentFileNumber As Integer
        Get
            Return (ReadValue("recentfilemaxnumber"))
        End Get
        Set(ByVal value As Integer)
            SaveValue("recentfilemaxnumber", Int(value))
            RaiseEvent FileUpdated(Me, 5)
        End Set
    End Property
    Public Sub AddRecentFile(ByVal Path As String)
        Dim pFile As XElement
        Dim pConfig As XElement = XElement.Load(Me.FilePath)
        Dim pCollection = From el In pConfig.<recentfiles>.<file>
                          Select el
                          Where el.Value = Path
        If pCollection.Count = 0 Then
            pFile = <file></file>
            pFile.Value = Path
            Dim pCollection2 = From el In pConfig.<recentfiles>
                           Select el
            Dim pStruttura As XElement = pCollection2(0)
            pStruttura.Add(pFile)
        Else
            pFile = pCollection(0)
        End If
        pFile.@date = Format(Now, "dd/MM/yyyy")
        pFile.@time = Format(Now, "HH:mm:ss")
        pConfig.Save(Me.FilePath)
        RaiseEvent FileUpdated(Me, 6)
    End Sub
    Public Sub RemoveRecenFile(ByVal Path As String)
        Dim pConfig As XElement = XElement.Load(Me.FilePath)
        Dim pCollection = From el In pConfig.<recentfiles>.<file>
                          Select el
                          Where el.Value = Path
        Dim pfile As XElement = pCollection(0)
        pFile.Remove()
        pConfig.Save(Me.FilePath)
        RaiseEvent FileUpdated(Me, 6)
    End Sub
    Public Sub Create()
        Dim FileStructure As XElement = <config>
                                            <behavior>
                                                <debug>false</debug>
                                            </behavior>
                                            <recentfiles>
                                                <recentfilemaxnumber>5</recentfilemaxnumber>
                                            </recentfiles>
                                            <lastdirs>
                                                <lastexportdir>c:\</lastexportdir>
                                                <lastopendir>c:\</lastopendir>
                                                <lastsavedir>c:\</lastsavedir>
                                            </lastdirs>
                                        </config>
        FileStructure.Save(Me.FilePath)
        RaiseEvent FileUpdated(Me, 1)
    End Sub
    Public Sub SaveValue(ByVal Key As String, ByVal value As String)
        Dim pElement As XElement
        pElement = XElement.Load(Me.FilePath)
        Dim pCollection = From el In pElement.Descendants.Elements(Key)
                        Select el
        If pCollection.Count > 0 Then
            Dim pItem As XElement = pCollection(0)
            pItem.Value = value
        End If
        pElement.Save(Me.FilePath)
    End Sub
    Public Function ReadValue(ByVal key As String) As String
        Dim pCollection = From el In XElement.Load(Me.FilePath).Descendants.Elements(key)
                  Select el.Value
        If pCollection.Count > 0 Then
            Return CStr(pCollection(0))
        Else
            Return ""
        End If
    End Function
End Class

